import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LogisticRegression


# 训练数据集
df_train = pd.read_csv('PythonMachineLearningAndPractice/data/section01/Breast-Cancer/breast-cancer-train.csv')
# 测试数据集
df_test= pd.read_csv('PythonMachineLearningAndPractice/data/section01/Breast-Cancer/breast-cancer-test.csv')
print(df_test)
# 选取Clump Thickness 和Cell Size作为特征, 构建测试集中的正负分类样本
df_test_negative = df_test.loc[df_test['Type'] == 0][['Clump Thickness','Cell Size']]
df_test_positive = df_test.loc[df_test['Type'] == 1][['Clump Thickness','Cell Size']]



# 绘制图1-2：良性肿瘤样本点，标记点为绿色 o
plt.scatter(df_test_negative['Clump Thickness'],df_test_negative['Cell Size'], marker='o',s=200, c='green')
# 绘制图1-2：恶性肿瘤样本点，标记点为给色x
plt.scatter(df_test_positive['Clump Thickness'], df_test_positive['Cell Size'], marker='x', s=150, c='red')
# 绘制x，y轴的说明
plt.xlabel('Clump Thickness')
plt.ylabel('Cell Size')
# 显示图
# plt.show()



# 利用NumPy中的random函数随机采样直线的截距和系数
# intercept = np.random.random([1])
# coef = np.random.random([2])
lx = np.arange(0, 12)
# ly = (-intercept-lx * coef[0]) / coef[1]
# plt.plot(lx, ly, c='yellow')
# plt.show()



# 绘图1-3
# plt.scatter(df_test_negative['Clump Thickness'], df_test_negative['Cell Size'], marker='o', s=200, c='red')
# plt.scatter(df_test_positive['Clump Thickness'], df_test_positive['Cell Size'], marker='x', s=150, c='black')
# plt.xlabel('Clump Thickness')
# plt.ylabel('Cell Size')
# plt.show()



# 传入sklearn中的逻辑回归分类器
# lr = LogisticRegression()
# lr.fit(df_train[['Clump Thickness', 'Cell Size']][:10], df_train['Type'][:10])
# print('Testing accuracy (10 training samples):', lr.score(df_test[['Clump Thickness', 'Cell Size']], df_test['Type']))
# intercept = lr.intercept_
# coef = lr.coef_[0, :]
# 原本这个分类页面应该是lx * coef[0] + ly * coef[1] + intercept=0， 映射到2维平面上之后，应该是：
# ly = (-intercept - lx * coef[0]) / coef[1]

# 绘制图1-4
# plt.plot(lx, ly, c='green')
# plt.scatter(df_test_negative['Clump Thickness'], df_test_negative['Cell Size'], marker='o', s=200, c='red')
# plt.scatter(df_test_positive['Clump Thickness'], df_test_positive['Cell Size'], marker='x', s=150, c='black')
# plt.xlabel('Clump Thickness')
# plt.ylabel('Cell Size')
# plt.show()



# 使用所有训练样本学习直线的系数和截距
lr = LogisticRegression()
lr.fit(df_test[['Clump Thickness', 'Cell Size']], df_test['Type'])
# print('Testing accuracy all samples:', lr.score(df_test_negative[['Clump Thickness', 'Cell Size']], df_test_positive['Type']))

intercept = lr.intercept_
coef = lr.coef_[0, :]
ly = (-intercept - lx * coef[0]) / coef[1]

#绘制图1-5
plt.plot(lx, ly, c='blue')
plt.scatter(df_test_negative['Clump Thickness'], df_test_negative['Cell Size'], marker='o', s=200, c='red')
plt.scatter(df_test_positive['Clump Thickness'], df_test_positive['Cell Size'], marker='x', s=150, c='black')
plt.xlabel('Clump Thickness')
plt.ylabel('Cell Size')
plt.show()






